<template>
  <div class="orgs">
    <a-alert
      v-if="alertVisable"
      showIcon
      banner
      message="您尚未加入任何单位"
      type="error"
      closable
      @close="alertOnClose"
    />
    <h4 style="margin-bottom: 16px">请选要管理的单位:</h4>
    <a-tag class="orgTag" v-for="item in orgs_000" :key="item.pid" @click="selectedOrg(item.pid, item.orgName,item.orgCode)" color="blue">{{ item.orgName }}</a-tag>
    <br/>
    <a-tag class="orgTag" v-for="item in orgs_002" :key="item.pid" @click="selectedOrg(item.pid, item.orgName,item.orgCode)" color="cyan">{{ item.orgName }}</a-tag>
  </div>
</template>

<script>
// import md5 from 'md5'
import storage from 'store'
import { selectOrg, getUserDepartments } from '@/api/login'
import { ORG, ORGNAME, DEPARTMENT } from '@/store/mutation-types'
import config from '@/config/defaultSettings'
// import { i18nRender } from '@/locales'

export default {
  components: {
  },
  data () {
    return {
      alertVisable: false, // 错误提示
      orgs_000: [],
      orgs_002: [],
      orgs: [],
      orgsCount: '1'
    }
  },
  created () {
    // this.getOrgsCount()
  },
  mounted () {
    this.alertOnClose = false
    this.getOrgsCount()
  },
  methods: {
    alertOnClose () {
      this.alertOnClose = false
    },
    getOrgsCount () {
      let count = 0
      const _this = this
      _this.orgs = []
      _this.orgs_000 = []
      _this.orgs_002 = []
      new Promise((resolve) => {
        resolve(selectOrg('org_000'))
      }).then((res) => {
        if (res && res.code === 'C1' && res.data) {
          for (const i in res.data) {
            count++
            const tn = {
              pid: res.data[i].org.pid,
              orgName: res.data[i].org.orgName,
              orgCode: res.data[i].org.orgCode
            }
            _this.orgs_000.push(tn)
            _this.orgs.push(tn)
          }
        }
      }).then(() => {
        selectOrg('org_002').then(res => {
          if (res && res.code === 'C1' && res.data) {
            for (const i in res.data) {
              count++
              const tn = {
                pid: res.data[i].org.pid,
                orgName: res.data[i].org.orgName,
                orgCode: res.data[i].org.orgCode
              }
              _this.orgs_002.push(tn)
              _this.orgs.push(tn)
            }
          }
          if (count === 0) {
            this.alertVisable = true
          }
          if (count === 1) {
            this.selectedOrg(this.orgs[0].pid, this.orgs[0].orgName,this.orgs[0].orgCode)
          }
        })
      }).then(() => {
        // console.log(count)
      })
    },
    getOrg000 () {
      const _this = this
      new Promise((resolve) => {
        resolve(selectOrg('org_000'))
      }).then(res => {
        if (res && res.code === 'C1' && res.data) {
          for (const i in res.data) {
            const tn = {
              pid: res.data[i].org.pid,
              orgName: res.data[i].org.orgName,
              orgCode: res.data[i].org.orgCode
            }
            _this.orgs_000.push(tn)
          }
        }
      })
    },
    getOrg002 () {
      const _this = this
      new Promise((resolve) => {
        resolve(selectOrg('org_002'))
      }).then(res => {
        if (res && res.code === 'C1' && res.data) {
          for (const i in res.data) {
            const tn = {
              pid: res.data[i].org.pid,
              orgName: res.data[i].org.orgName,
              orgCode: res.data[i].org.orgCode
            }
            _this.orgs_002.push(tn)
          }
        }
      })
    },
    selectedOrg (pid, orgName,orgCode) {
      
      // const _this = this
      // console.log(pid)
      if (pid) {
        new Promise((resolve) => {
          storage.remove(ORG)
          storage.set(ORG, pid)
          storage.remove(ORGNAME)
          storage.set(ORGNAME, orgName)
          storage.set('orgCode', orgCode)
          console.log(orgCode);
          console.log(orgName, orgName)
          config.title = orgName
          // const { title } = _this.$route.meta
          // title && (setDocumentTitle(`${i18nRender(title)} - ${domTitle}`))
          // _this.$i18n.getLocaleMessage(_this.$store.getters.lang).antLocale
          resolve()
        }).then(() => {
          getUserDepartments(pid)
            .then(res => {
              if (res && res.code && res.code === 'C1' && res.data) {
                const departs = []
                for (let i = 0; i < res.data.length; i++) {
                  departs.push(res.data[i].depart)
                }
                storage.remove(DEPARTMENT)
                storage.set(DEPARTMENT, departs)
              }
            })
        })
        setTimeout(() => {
          this.$router.replace({ path: '/' })
        }, 100)
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .orgTag {
    width: 280px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    font-size: 20pt;
    margin-left: 10px;
    margin-right: 10px;
    margin-bottom: 10px;
  }
  .orgs {
    text-align: center;
    width: 1200px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 10px;
  }
.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
